
package com.oahcfly.chgame.core.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.utils.Align;
import com.oahcfly.chgame.core.Chao;
import com.oahcfly.chgame.core.freefont.FreeFont;
import com.oahcfly.chgame.core.freefont.FreeLabel;

/** Toast提示语
 * 
 * @author haocao */
public class CHToastManager {

	private final static float cx = Chao.gameW / 2;
	private final static float cy = Chao.gameH / 2;

	public static void showTipAtCenter (String text) {
		showTip(text, Color.RED, cx, cy, 2.5f, null);
	}

	public static void showTipAtCenter (String text, Color color) {
		showTip(text, color, cx, cy, 2, null);
	}

	public static void showTipAtCenter (String text, float duration) {
		showTip(text, Color.RED, cx, cy, duration, null);
	}

	public static void showTip (String text, Color txtColor, float tcx, float tcy, float seconds, final Runnable callbackRunnable) {

		final FreeLabel freeLabel = FreeFont.getLabel(txtColor, text);

		if (freeLabel.getWidth() > 400) {
			freeLabel.setWidth(400);
			freeLabel.setWrap(true);
		}
		freeLabel.setPosition(tcx, tcy, Align.center);

		Chao.game.getScreen().addActor(freeLabel);

		Action parallelAction = Actions.parallel(Actions.moveBy(0, freeLabel.getHeight() * 2, seconds),
			Actions.alpha(0, seconds, Interpolation.circle));
		Action seqAction = Actions.sequence(parallelAction, Actions.run(new Runnable() {

			@Override
			public void run () {
				freeLabel.remove();
				if (callbackRunnable != null) {
					callbackRunnable.run();
				}
			}
		}));

		freeLabel.addAction(Actions.delay(0.2f, seqAction));
	}

}
